class Solution {
public:
    bool isPalindrome(ListNode*head) {
        ListNode*preNode=head;
        ListNode*reverseList=nullptr;
        int count=0;
        while(preNode)
        {
            count++;
            preNode = preNode->next;
        }
        for (int i = 0;i<count/2;i++)
        {
            preNode = head;
            head = head -> next;
            preNode->next = reverseList;
            reverseList = preNode;
        }
        if (count%2 == 1)
            head = head->next;
        for (int i = 0;i<count/2;i++)
        {
            if(reverseList->val != head->val)
                return false;
            reverseList = reverseList->next;
            head = head->next;
        }
        return true;
    } 
};